<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select
        v-model="listQuery.modelId"
        clearable
        class="filter-item"
        style="width: 160px"
        placeholder="选择模块"
      >
        <el-option
          v-for="item in modelListOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-input
        v-model="listQuery.personName"
        clearable
        class="filter-item"
        style="width: 160px"
        placeholder="人员名称"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查找</el-button
      >
      <!-- <el-button
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="handleDownload"
        >导出</el-button
      > -->
    </div>
    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      :span-method="objectSpanMethod"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="担任角色" prop="personDuty">
        <template slot-scope="scope">
          <span
            >{{ personDutyObjNameMap[scope.row.personDuty]
            }}<span>（{{ scope.row.personDutyCount }}个）</span></span
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="项目模块"
        prop="owningModuleName"
      />
      <el-table-column align="center" label="项目/成果名称" prop="projectName">
        <template slot-scope="scope">
          <span
            style="color: #409eff; cursor: pointer"
            v-if="scope.row.projectName"
            @click="jumpModel(scope.row)"
          >
            {{ scope.row.projectName }}
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="部门" prop="personDepartment">
      </el-table-column>
      <el-table-column align="center" label="职称" prop="personJobTitle" />
      <el-table-column align="center" label="学历" prop="personLearnLevel" /> -->
      <el-table-column align="center" label="排名" prop="personRank" />
      <el-table-column align="center" label="录入时间" prop="insertTime" />
      <el-table-column align="center" label="备注" prop="personRemark" />
    </el-table>
    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    /> -->
    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>
  </div>
</template>

<script>
import { listPerformance } from '@/api/contribution-query/index'
import { modelListOptions, personDutyObjNameMap } from '@/utils/commonData'
import BackToTop from '@/components/BackToTop'

export default {
  name: 'GoodsList',
  components: { BackToTop },
  data() {
    return {
      modelListOptions,
      personDutyObjNameMap,
      rowSpanArr: [],
      list: [],
      total: 0,
      listQuery: {
        modelId: -1,
        personName: ''
      },
      owningModuleUrlMap: {
        0: '/technology-project/project-manage',
        1: '/technology-result/journal-papers',
        2: '/technology-result/works',
        3: '/technology-result/technology-awards',
        4: '/technology-result/major',
        5: '/technology-result/software',
        6: '/technology-result/standard',
        7: '/technology-result/system-plan-report-programme',
        8: '/technology-result/seismograph',
        9: '/technology-result/popular-science',
        10: '/technology-result/short-impending',
        11: '/technology-result/other',
        12: '/field-exploration',
        13: '/field-exploration-result'
      },
      owningModuleNameUrlMap: {
        科研业务项目: '/technology-project/project-manage',
        期刊论文: '/technology-result/journal-papers',
        论著: '/technology-result/works',
        科技奖励: '/technology-result/technology-awards',
        专利: '/technology-result/major',
        软件著作权: '/technology-result/software',
        标准: '/technology-result/standard',
        '制度/规划/报告/方案等':
          '/technology-result/system-plan-report-programme',
        '地震仪器样机/软件': '/technology-result/seismograph',
        科普作品: '/technology-result/popular-science',
        短临预报: '/technology-result/short-impending',
        其他成果: '/technology-result/other',
        野外探测部分: '/field-exploration',
        野外工作成果: '/field-exploration-result'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listPerformance(this.listQuery)
        .then((response) => {
          let resData = { ...response.data }
          this.list = [...resData[1], ...resData[2], ...resData[3]]
          this.total = resData[1].length + resData[2].length + resData[3].length
          this.rowSpanArr = []
          let itemIndex = 0
          this.list.map((el, index) => {
            if (index == 0) {
              this.rowSpanArr.push(1)
            } else {
              if (
                this.list[index].personDuty == this.list[index - 1].personDuty
              ) {
                this.rowSpanArr[itemIndex] += 1 //项目/成果名称相同，合并到同一个数组中
                this.rowSpanArr.push(0)
              } else {
                this.rowSpanArr.push(1)
                itemIndex = index
              }
            }
            if (el.personDuty === 1) {
              el.personDutyCount = resData[1].length
            } else if (el.personDuty === 2) {
              el.personDutyCount = resData[2].length
            } else if (el.personDuty === 3) {
              el.personDutyCount = resData[3].length
            }
            return el
          })
          this.listLoading = false
        })
        .catch((response) => {
          if (response.data) {
            let resData = { ...response.data }
            this.list = [...resData[1], ...resData[2], ...resData[3]]
            this.total =
              resData[1].length + resData[2].length + resData[3].length
            this.rowSpanArr = []
            let itemIndex = 0
            this.list.map((el, index) => {
              if (index == 0) {
                this.rowSpanArr.push(1)
              } else {
                if (
                  this.list[index].personDuty == this.list[index - 1].personDuty
                ) {
                  this.rowSpanArr[itemIndex] += 1 //项目/成果名称相同，合并到同一个数组中
                  this.rowSpanArr.push(0)
                } else {
                  this.rowSpanArr.push(1)
                  itemIndex = index
                }
              }
              if (el.personDuty === 1) {
                el.personDutyCount = resData[1].length
              } else if (el.personDuty === 2) {
                el.personDutyCount = resData[2].length
              } else if (el.personDuty === 3) {
                el.personDutyCount = resData[3].length
              }
              return el
            })
            this.listLoading = false
          }
        })
    },
    jumpModel(row) {
      let jumpUrl =
        row.owningModule != undefined
          ? this.owningModuleUrlMap[row.owningModule]
          : this.owningModuleNameUrlMap[row.owningModuleName]
      let newpage = this.$router.resolve({
        path: jumpUrl,
        query: {
          projectName: row.projectName
        }
      })
      window.open(newpage.href, '_blank')
      // this.$router.push({
      //   path: jumpUrl,
      //   query: { projectName: row.projectName }
      // })
    },
    handleFilter() {
      this.getList()
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.rowSpanArr[rowIndex]
        return {
          rowspan: _row, //行
          colspan: 1 //列
        }
      }
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then((excel) => {
        const tHeader = [
          '商品ID',
          '商品编号',
          '名称',
          '专柜价格',
          '当前价格',
          '是否新品',
          '是否热品',
          '是否在售',
          '首页主图',
          '宣传图片列表',
          '商品介绍',
          '详细介绍',
          '商品图片',
          '商品单位',
          '关键字',
          '类目ID',
          '品牌商ID'
        ]
        const filterVal = [
          'id',
          'goodsSn',
          'name',
          'counterPrice',
          'retailPrice',
          'isNew',
          'isHot',
          'isOnSale',
          'listPicUrl',
          'gallery',
          'brief',
          'detail',
          'picUrl',
          'goodsUnit',
          'keywords',
          'categoryId',
          'brandId'
        ]
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '商品信息')
        this.downloadLoading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  .el-table {
    .status-audit {
      position: relative;
      font-weight: bold;
      &::before {
        content: '•';
        font-size: 24px;
        color: inherit;
        position: absolute;
        top: -4px;
        left: -14px;
      }
    }
  }
}
</style>
<style>
.table-expand {
  font-size: 0;
}

.table-expand label {
  width: 100px;
  color: #99a9bf;
}

.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}

.gallery {
  width: 80px;
  margin-right: 10px;
}

.goods-detail-box img {
  width: 100%;
}
</style>
